package com.uenpay.oem.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.ui.old.activity.PushServiceConn;
import com.uenpay.oem.R;
import com.uenpay.oem.common.RxBusEventConstant;
import com.uenpay.oem.ui.user.login.LoginActivity;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.rx.CommonEvent;
import com.uenpay.util.rx.RxBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by PS on 2017/6/22.
 */

public class SettingsPswActivity extends BaseActivity implements SettingsContract.View {
    @BindView(R.id.tv_phonecode)
    TextView tvPhonecode;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.loading)
    View loading;
    @BindView(R.id.tv_anew)
    TextView tvanew;
    @BindView(R.id.iv_pswyes)
    ImageView ivpswyes;
    private SettingsPresenter mseetingpresenter;
    private String phonenumber;


    public Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message arg0) {
            if (arg0.what == 0) {
                if (loading != null)
                    loading.setVisibility(View.GONE);
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initListener() {
        //名称的监听
        edName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String content = edName.getText().toString();
                if (Utils.String_length(content) > 30) {
                    if (content.length() > 15) {
                        edName.setText(content.substring(0, 15));
                        edName.setSelection(edName.getText().toString().length());
                    }
                    return;
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_seetingpsw;
    }

    @Override
    public void initView(View view) {
        setTitle("设置密码");
        phonenumber = getIntent().getStringExtra("account_name");
        edPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        tvPhonecode.setText(phonenumber);
        mseetingpresenter = new SettingsPresenter(this, this);
        initListener();
    }


    private void chekInfo(String name, String edcode, String password) {
        if (TextUtils.isEmpty(name)) {
            IMApplication.showToast(getResources().getString(
                    com.uenpay.im.R.string.phone_number_error));
            return;
        } else if (TextUtils.isEmpty(edcode)) {
            IMApplication.showToast(getResources().getString(
                    com.uenpay.im.R.string.phone_number_error));
            IMApplication.showToast(getResources().getString(
                    com.uenpay.im.R.string.code_number_not_null));
            return;
        } else if (TextUtils.isEmpty(password)) {
            IMApplication.showToast(getResources().getString(
                    com.uenpay.im.R.string.pwd_not_null));
            return;
        }
    }

    /**
     * 检查密码长度
     *
     * @param pwd
     * @return
     */
    private boolean checkPwdLength(String pwd) {
        if (pwd.length() < 6 || pwd.length() > 16) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void setPresenter(Object presenter) {

    }

    @Override
    public void showToast(String msg) {

    }

    // 注册成功登陆页面
    @Override
    public void preparelogin() {

        PushServiceConn.getInstance(SettingsPswActivity.this).startConn();
        setResult(RESULT_OK);

        finish();

    }

    boolean ispswyes = false;

    @OnClick({R.id.tv_anew, R.id.btn_accomplish, R.id.iv_pswyes})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_anew:
                RxBus.$().post(ReglsterActivitycopy.TAG,new CommonEvent(RxBusEventConstant.EVENT_REGISTER_CODE));
                if (timer != null) {
                    timer.start();
                }

                tvanew.setEnabled(false);
                break;
            case R.id.btn_accomplish:

                Login();
                break;

            case R.id.iv_pswyes:
                if (ispswyes) {
                    ispswyes = true;
                    edPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    ivpswyes.setBackgroundResource(R.drawable.icon_closeeyes);
                } else {
                    edPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    ivpswyes.setBackgroundResource(R.drawable.icon_openeyes);
                }
                ispswyes = !ispswyes;
                break;
        }
    }

    CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            tvanew.setText("(" + (millisUntilFinished / 1000) + "s)");
        }

        @Override
        public void onFinish() {
            tvanew.setEnabled(true);
            tvanew.setText(getResources().getString(com.uenpay.im.R.string.send_code));
        }

    };

    private void Login() {
        String edcode = edCode.getText().toString().trim();
        String password = edPassword.getText().toString().trim();
        String name = edName.getText().toString().trim();

        if (!TextUtils.isEmpty(edcode) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(name)) {
            if (!checkPwdLength(password)) {
                IMApplication.showToast(getResources().getString(
                        com.uenpay.im.R.string.pwd_length));
                return;
            }
            boolean mat = password.matches("^(\\s|.*\\s+.*)$");
            if (mat) {
                IMApplication.showToast(getResources().getString(
                        com.uenpay.im.R.string.phone_and_pwd_not_null));
                return;
            }
            if (password.getBytes().length != password.length()) {
                IMApplication
                        .showToast(getResources()
                                .getString(
                                        com.uenpay.im.R.string.pwd_can_not_contain_chinese_characters));
                return;
            }
            //注册
            loading.setVisibility(View.VISIBLE);
            mseetingpresenter.reglsterSuccess(name, phonenumber, password, edcode);

        } else {
            chekInfo(name, edcode, password);
        }


    }
}

